<%@ page language="java" import="java.util.*,com.gdcn.kpi.common.attach.model.*,com.gdcn.kpi.common.SpringHelper,com.gdcn.kpi.common.attach.service.IAttachService" pageEncoding="UTF-8"%>
<jsp:directive.page import="com.gdcn.kpi.system.service.IUserService"/>
<jsp:directive.page import="com.gdcn.kpi.system.valueobject.Station"/>
<jsp:directive.page import="com.gdcn.kpi.system.valueobject.Group"/>
<%@ include file="../head.inc" %>
<%!
	private static void getUserGroups(Group group,Map map){
    	if(group!=null){
    		map.put(group.getId(), group.getId());
    		getUserGroups(group.getParentGroup(),map);
    	}
    }
    private static boolean isEquals(int type,AttachAuth attachAuth){
		if(type==1){
			if(attachAuth.getPread()==1) return true;
		}
		if(type==2){
			if(attachAuth.getPdelete()==2) return true;
		}
		return false;
    }
	public static boolean isAuthorizationForAttach(String userId,List attachAuthList,int type){
		IUserService userService = (IUserService)SpringHelper.getBean("userService");
    	User user = userService.findUserById(userId);
    	Station station = user.getStation();
    	Group group = user.getGroup();
    	Map map = new HashMap();
    	getUserGroups(group,map);
    	for(int i=0;i<attachAuthList.size();i++){
    		AttachAuth attachAuth = (AttachAuth) attachAuthList.get(i);
    		if(attachAuth.getWasType()==Globals.GROUP){
    			if(map.get(attachAuth.getWasId())!=null){
    				if(isEquals(type,attachAuth))return true;
    			}
    		}
    		if(attachAuth.getWasType()==Globals.USER){
    			if(attachAuth.getWasId().equals(user.getId())) {
    				if(isEquals(type,attachAuth))return true;
    			}
    		}
    		if(attachAuth.getWasType()==Globals.STATION){
    			if(station!=null&&attachAuth.getWasId().equals(station.getId())) {
    				if(isEquals(type,attachAuth))return true;
    			}
    		}
    	}
    	return false;
    }
    
    public static boolean haveAuthorizationReadForAll(List attachAuthList){
    	for(int i=0;i<attachAuthList.size();i++){
    		AttachAuth attachAuth = (AttachAuth) attachAuthList.get(i);
    		if(attachAuth.getPread()==1) return false;
    	}
    	return true;
    }
%>
<%String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>
<html>
	<head>
		<base href="<%=basePath%>"><title>附件列表显示</title>
	</head>
	<%
String idstr = request.getParameter("id");
String isupload = request.getParameter("isupload");
String download = request.getParameter("download");
String delete = request.getParameter("delete");
if(isupload==null||isupload.equals("")){
isupload = "true";
}
if(download==null||download.equals("")){
download = "false";
}
if(delete==null||delete.equals("")){
delete = "false";
}

IAttachService attachService = (IAttachService)SpringHelper.getBean("attachService");
List allList = new ArrayList();
String[] ids = idstr.split(",");
for(int i=0;i<ids.length;i++){
String id = ids[i];
		List attachLists = attachService.listAttachs(id);
		allList.addAll(attachLists);
		}
		request.setAttribute("attachList", allList);
%>
<script language="javascript">
function attachUpload(){
	var table = document.getElementById("atabPro");
	var returnValue = window.showModalDialog('<%=request.getContextPath()%>/common/upload/upload.jsp?resourceId=<%=ids[0]%>',table,'dialogHeight:300px;dialogWidth:660px;status=off');

}

function deleteAttach(id){
if (confirm("您确认要删除记录？")) {
var url = "<%=request.getContextPath()%>/deleteAttachFile.do?id="+id;
LoadAjaxContent(url,callBackMethod);
}
}

function callBackMethod(){
	eval(this.request.responseText);
	if(result!=""){
		var grid = document.getElementById("atabPro");
		for(var i = 0; i < grid.rows.length; i++)
		{
		  	if(grid.rows[i].id == result)
		  	{
		  		grid.deleteRow(i);
		  	}
		};
	}
}

function writeBack(){
	//这个方法将页面上附件数量的改变写回主界面
	var cell = window.dialogArguments;
	if(cell){
		var count = document.getElementById("atabPro").rows.length-1;
		if(count==0){
			cell.innerHTML="<a href=\"javascript:ev_attachs('<%=idstr%>','"+cell.id+"')\">没有附件</a>";
		}else {
			cell.innerHTML="<a href=\"javascript:ev_attachs('<%=idstr%>','"+cell.id+"')\">" + 
					"<font color=red >"+count+"</font>个附件</a>";
		}
	}
}

</script>

	<body onunload="writeBack();" >
	<form name="form2" method="post" action="">

	<table width="95%" border="0" align="center">
  	<tr>
    	<td bgcolor="#FFFFFF" width="100%">
     	<table width="100%" bordercolor=#c0c0c0 bordercolordark=#ffffff align="center"
      	bordercolorlight=#B3BBC8 border=1 cellpadding="2" cellspacing="0" id=atabPro width="100%">
    		<tr>
				<td class=header align="center" width="28%" height="20" nowrap="nowrap">
					附件名称
				</td>
				<td class=header align="center" width="30%" height="20" nowrap="nowrap">
					上传人
				</td>
				<td class=header align="center" width="15%" height="20" nowrap="nowrap">
					上传时间
				</td>
				<td class=header align="center" width="12%" nowrap="nowrap">
					附件大小
				</td>
				<td class=header align="center" width="15%" nowrap="nowrap">
					操作
				</td>
			</tr>
			<logic:iterate id="attachList" name="attachList" >
				<tr onmouseover='this.style.backgroundColor="#EFEFEF"' onmouseout='this.style.backgroundColor="#ffffff"' bgColor=#ffffff
					id="<bean:write name="attachList" property="id" />" >
					<td align="left" height="20" >
						<bean:write name="attachList" property="name" />
					</td>
					<td align="left" height="20" >
						<bean:write name="attachList" property="creatorInfo" />
					</td>
					<td align="center" height="20" >
						<bean:write name="attachList" property="genDate" format="yyyy-MM-dd"/>
					</td>
					<%--<td align="left">
						<span style="color:#FF0000">可读:</span>
						<logic:iterate id="attachAuth" name="attachList" property="attachAuthSet">
							<logic:equal name="attachAuth" property="pread" value="1">
								<bean:write name="attachAuth" property="wasName" />
							</logic:equal>
						</logic:iterate>
						<span style="color:#FF0000">删除:</span>
						<logic:iterate id="attachAuth" name="attachList" property="attachAuthSet">
							<logic:equal name="attachAuth" property="pdelete" value="2">
								<bean:write name="attachAuth" property="wasName" />
							</logic:equal>
						</logic:iterate>
					</td>
					--%><td align="center">
						<bean:write name="attachList" property="fileSize" />
						KB
					</td>
					<td align="center">
					<%
					Attach attach = (Attach) attachList;
					List attachAuthList = new ArrayList(attach.getAttachAuthSet());
					if(attach.getFlgOpenAll()==1){
					%>
					<a href="<%=request.getContextPath() %>/downloadFile.do?id=<bean:write name="attachList" property="id"/>"><img src="<%=request.getContextPath()%>/images/download.gif" border="0"></a> 
					<%
					}else{
					if(isAuthorizationForAttach(loginedUser.getId(),attachAuthList,1)){
					%>
						<a href="<%=request.getContextPath() %>/downloadFile.do?id=<bean:write name="attachList" property="id"/>"><img src="<%=request.getContextPath()%>/images/download.gif" border="0"></a> 
						<%
						}else{
						out.print("&nbsp;");
						}
						}
						if(isAuthorizationForAttach(loginedUser.getId(),attachAuthList,2)){
						%>
						<a onclick="deleteAttach('<bean:write name="attachList" property="id" />');"><img
								src="<%=request.getContextPath()%>/images/delete.gif" border="0" style="CURSOR: hand"></a>
								<%
								}else{
								out.print("&nbsp;");
								}
								%>
					</td>
				</tr>
			</logic:iterate>
		</table></td></tr>		
		<%if(isupload.equalsIgnoreCase("true")){%>
		<tr><td>
		<input type='button' name='add_attach_button' onclick="javascript:attachUpload()" value='上传附件' class="button"/>
		</td></tr>
		<%}%>
	</table>
	</form>
	</body>
</html>
